﻿using AutoMapper;
using Microsoft.AspNetCore.Http;
using Rbac.Contracts.DTO.InPut;
using Rbac.Contracts.DTO.OutPut;
using Rbac.Contracts.Interface;
using Rbac.Domain.Entity;
using Rbac.Domain.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Rbac.Server
{
	/// <summary>
	/// 角色实现
	/// </summary>
	public class RoleServer :ServerBase, IRoleServer
	{
		private readonly IRoleRepository _roleRepository;
		public RoleServer(IRoleRepository roleRepository)
		{
			_roleRepository = roleRepository;
		}
		/// <summary>
		/// 角色查询
		/// </summary>
		/// <param name="roleName"></param>
		/// <returns></returns>
		/// <exception cref="NotImplementedException"></exception>
		public List<RoleOutPut> SearchRoles(string roleName)
		{
			var roles= _roleRepository.SearchRoles(roleName);
			return _mapper.Map<List<RoleOutPut>>(roles);
		}
		/// <summary>
		/// 添加
		/// </summary>
		/// <param name="role"></param>
		/// <returns></returns>
		public int Add(RoleInput role)
		{
			var entity = _mapper.Map<Role>(role);
			entity.CreatedTime = DateTime.Now;
			entity.UpdatedTime = DateTime.Now;
			entity.CreatedUserId = LoginUserId;
			entity.IsDeleted = false;
			return _roleRepository.Add(entity);
		}
		/// <summary>
		/// 修改
		/// </summary>
		/// <param name="role"></param>
		/// <returns></returns>
		public int Update(RoleInput role)
		{
			return _roleRepository.Update(_mapper.Map<Role>(role));
		}
		/// <summary>
		/// 删除
		/// </summary>
		/// <param name="idList"></param>
		/// <returns></returns>
		public int Delete(params int[] idList)
		{
			var loginUserId = Convert.ToInt32(_contextAccessor.HttpContext.User.Claims.FirstOrDefault(c => c.Type.Equals("UserId")).Value);
			return _roleRepository.Delete(loginUserId, idList);
		}
	}
}
